package example.tests;

import junit.framework.*;
import java.util.*;

import example.*;

public class LetterGraphFinderTest extends TestCase {

    private LetterGraphFinder finder;

    public LetterGraphFinderTest(String s) {
        super(s);
    }

    protected void setUp() {
        // Set up the graph before each test (this practive avoids side effect)
        LetterGraphBuilder builder = new LetterGraphBuilder(5);
        builder.addPath('A', 'B', 5);
        builder.addPath('B', 'C', 4);
        builder.addPath('C', 'B', 2);
        builder.addPath('C', 'D', 7);
        builder.addPath('E', 'C', 5);
        builder.addPath('D', 'A', 3);
        builder.addPath('D', 'E', 5);

        // Instantiates the finder
        finder = new LetterGraphFinder(builder.getLetterGraph());
    }

    protected void tearDown() {
    }

    public void testFindShortestPath() {

        assertEquals("A-B-C-D", finder.findShortestPath('A', 'D'));

        assertEquals("C-D-E", finder.findShortestPath('C', 'E'));

    }

    public void testFindPathsWithMaximumDistance() {
        ArrayList solutions = (ArrayList) finder.findPathsWithMaximumDistance('A', 'C', 20);

        // Iterate on the solutions, increment counter each time a correct solution is found
        int numberOfCorrectSolutions = 0;
        Iterator iter = solutions.iterator();
        while (iter.hasNext()) {
            String sol = iter.next().toString();
            if (sol.equals("A-B-C") || sol.equals("A-B-C-B-C")) {
                numberOfCorrectSolutions++;
            }
        }
        // How many solutions found ?
        assertEquals(2, numberOfCorrectSolutions);
    }

}